<template>
  <div>
    <div class="ke__margin" :style="{ width: '600px' }">
      <k-form label-width="100px" :data="form" @submit="onSubmit">
        <k-form-item label="原密码" prop="old" :validate="['required']">
          <el-input v-model="form.old" type="password"/>
        </k-form-item>

        <k-form-item label="新密码" prop="pwd" :validate="['required']">
          <el-input v-model="form.pwd" type="password"/>
        </k-form-item>

        <k-form-item label="确认密码" prop="repwd" :validate="['required']">
          <el-input v-model="form.repwd" type="password"/>
        </k-form-item>

        <k-form-item>
          <el-button type="primary" native-type="submit" :loading="subing">确认修改</el-button>
        </k-form-item>
      </k-form>
    </div>
  </div>
</template>

<script>
import { Vue, Component } from 'vue-property-decorator'
@Component
export default class Editpwd extends Vue {
  form = {}
  subing = false

  onSubmit () {
    if (this.form.pwd !== this.form.repwd) {
      this.$message.error('二次密码不一致')
      return
    }
    this.subing = true
    this.$http.post('admin/changepassword', {
      old: this.form.old,
      pwd: this.form.pwd
    })
      .finally(() => this.subing = false)
      .then((result) => {
        if (result.res_code === 0) {
          this.$message.success('密码修改成功')
          this.$store.dispatch('auth/logout')
          this.$router.push({ name: 'login' })
        } else {
          this.$message.error(result.res_msg)
        }
      })
  }
}
</script>

<style scoped>

</style>
